import pandas as pd
import requests
from io import StringIO
from calc_manager.base.plugins import PluginObject

class CsvPlugin(PluginObject):
    def __init__(self, osspath: str):
        self.df = load_and_print_csv_from_oss(osspath)
        self.data = 4
        print("csv plugin init......")
    
    def print_df_info(self):
        print(self.df.info())
    
    def print_df_head(self):
        print(self.df.head())


def load_and_print_csv_from_oss(oss_url):
    try:
        response = requests.get(oss_url)
        response.raise_for_status()  # 确保请求成功

        csv_data = StringIO(response.text)
        dataframe = pd.read_csv(csv_data)
        return dataframe

    except requests.RequestException as e:
        print(f"request oss url {oss_url} failed: {e}")
    except pd.errors.ParserError as e:
        print(f"Pandas parse csv error: {e}")